//
//  MLMainViewController.swift
//  SinaWeibo
//
//  Created by 李亮亮 on 16/4/6.
//  Copyright © 2016年 Big Fish. All rights reserved.
//

import UIKit

class MLMainViewController: UITabBarController {

    override func viewDidLoad() {
        super.viewDidLoad()
        
        
        // Do any additional setup after loading the view.
        // 自定义tabBar
        let mainTabBar = MLMainTabBar()
        mainTabBar.plusBtn.addTarget(self, action: #selector(MLMainViewController.plusBtnClick), forControlEvents: .TouchUpInside)
        
        // 使用KVC
//        print(tabBar.classForCoder) // 打印tabBar的类型
        self.setValue(mainTabBar, forKey: "tabBar")
//        print(tabBar.classForCoder)
        
        // 添加子视图控制器
        addChildViewControllers()
        
    }
    
    @objc func plusBtnClick() {
        print("shit")
    }
    
    
    // MARK: - 添加子视图控制器
    // private 只有在 '当前文件内' 才可以访问该方法
    private func addChildViewControllers() {
        
        addChildViewController(MLHomeTableViewController(), title: "首页", imageName: "tabbar_home")
        addChildViewController(MLMessageTableViewController(), title: "消息", imageName: "tabbar_message_center")
        addChildViewController(MLDiscoverTableViewController(), title: "发现", imageName: "tabbar_discover")
        addChildViewController(MLProfileTableViewController(), title: "我", imageName: "tabbar_profile")
        
    }
    
    private func addChildViewController(vc: UIViewController, title: String, imageName: String) {
        
        // 设置tabbarItem的title
        // 导航条的title和tabbarItem的title显示不一样的时候 可以使用这种方式
//        vc.tabBarItem.title = title
//        vc.navigationItem.title = title
        vc.title = title
        vc.tabBarItem.image = UIImage(named: imageName)
        vc.tabBarItem.selectedImage = UIImage(named: imageName + "_selected")?.imageWithRenderingMode(UIImageRenderingMode.AlwaysOriginal)
        vc.tabBarItem.setTitleTextAttributes([NSForegroundColorAttributeName: UIColor.orangeColor()], forState: .Selected)
//        tabBar.tintColor = UIColor.orangeColor()
        
        let nav = MLBaseNavigationController(rootViewController: vc)
        addChildViewController(nav)
    
    }
    
    override func viewDidAppear(animated: Bool) {
        super.viewWillDisappear(animated)
    }
    
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
